package com.api.customization.huangwm.dao.SW003;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.customization.huangwm.utils.DateTools;
import com.customization.huangwm.utils.LogTool;
import weaver.conn.RecordSetTrans;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;

import java.util.Map;

/**
 * @Author Huangwm
 * @Date 2022-05-04
 * @Version 1.0
 * @Description TODO 记录日志
 **/
public class LoggingDao {
    private LogTool log = new LogTool("/log/SW003/huangwm/dao/LoggingDao", false);
    private DateTools dateTools = new DateTools();

    /**
     * 接口調用日志
     * @param source 0或1 分別為eap内部調用和外部系統調用
     * @param url
     * @param params 調用參數json字符串
     * @param resultMsg
     */
    public void wrtieLog(String source, String url, String params, String resultMsg) {
        BaseBean bean = new BaseBean();
        String table = "uf_003_interfaceLog";
        String logTable_Modeid = bean.getPropValue("SW003", "logTable_Modeid");
        logTable_Modeid = logTable_Modeid.isEmpty() ? "70662" : logTable_Modeid;
        RecordSetTrans rs = new RecordSetTrans();
        rs.setAutoCommit(false);
        String sql = "insert into " + table + "(formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime,MODEUUID,source,url,params,resultMsg)values(?,?,?,?,?,?,?,?,?,?)";
        try {
            Object[] objects = new Object[]{
                    logTable_Modeid, 1, 0, dateTools.getCurrentDate(), dateTools.getCurrentTime(), dateTools.getUUID(),
                    source, url, params, resultMsg
            };
            log.writeLog("接口調用日志");
            log.writeLog(JSONObject.toJSONString(objects));
            rs.executeUpdate(sql, objects);
        } catch (Exception e) {
            rs.rollback();
            e.getStackTrace();
        }
        rs.commit();
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setModeId(Integer.parseInt(logTable_Modeid));
        modeRightInfo.setNewRight(true);
        //...
    }

    /**
     * 出入庫日志
     * @param source  0或1 分別為eap内部調用和外部系統調用
     * @param assetCode 物料編碼
     * @param quantity 出入庫數量
     * @param requestNumber 單號
     * @param reason 原因
     * @param field1 仓库
     * @param field2 货架
     * @param userid 使用人
     * @param operator 操作人(申请人)
     */
    public void wrtieInventoryLog(RecordSetTrans rs,String source, String assetCode, Double quantity, String requestNumber,
                                  String reason,String field1, String field2,String type,String userid,String operator,
                                  String assetName) {
        BaseBean bean = new BaseBean();
        String table = "uf_003_inventoryLog";
        String logTable_Modeid = bean.getPropValue("SW003", "inventoryLogTable_Modeid");
        logTable_Modeid = logTable_Modeid.isEmpty() ? "70663" : logTable_Modeid;
        //使用流程的rst
//        RecordSetTrans rs = new RecordSetTrans();
//        rs.setAutoCommit(false);
        String sql = "insert into " + table + "(formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime,MODEUUID," +
                "source,assetCode,quantity,requestNumber,reason,field1,field2,type,dateStr,userid,operator,assetname)" +
                "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            log.writeLog("出入庫日志");
            Object[] objects = new Object[]{
                    logTable_Modeid, 1, 0, dateTools.getCurrentDate(), dateTools.getCurrentTime(), dateTools.getUUID(),
                    source, assetCode, quantity, requestNumber, reason,field1, field2,type,dateTools.getCurrentDate(),
                    userid,operator,assetName
            };
            log.writeLog(JSONObject.toJSONString(objects));
            rs.executeUpdate(sql, objects);
        } catch (Exception e) {
            //rs.rollback();
            e.getStackTrace();
            throw  new RuntimeException("寫入日志表異常 "+e.getMessage());
        }
        //rs.commit();
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setModeId(Integer.parseInt(logTable_Modeid));
        modeRightInfo.setNewRight(true);
        //...
    }
}
